package en.edu.zxj.springrabbimqdemo.config;

import en.edu.zxj.springrabbimqdemo.constant.Constants;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created with IntelliJ IDEA.
 * Description：
 *
 * @author: zxj
 * @date: 2024-09-20
 * @time: 14:59:09
 */
@Configuration
public class RabbitMQDirectConfig {
    // 1. 声明 direct exchange
    @Bean("directExchange")
    public DirectExchange directExchange() {
        return ExchangeBuilder.directExchange(Constants.DIRECT_EXCHANGE_NAME).durable(true).build();
    }

    // 2. 声明队列
    @Bean("directQueue1")
    public Queue directQueue1() {
        return QueueBuilder.durable(Constants.DIRECT_QUEUE_NAME1).build();
    }

    @Bean("directQueue2")
    public Queue directQueue2() {
        return QueueBuilder.durable(Constants.DIRECT_QUEUE_NAME2).build();
    }

    // 3. 绑定队列和交换机
    // 给队列1绑定 routingKey 为 orange
    @Bean("directBinding1")
    public Binding directBinding1(@Qualifier("directQueue1") Queue directQueue1, @Qualifier("directExchange") DirectExchange directExchange) {
        // 绑定队列和交换机
        return BindingBuilder.bind(directQueue1).to(directExchange).with("orange");
    }
    // 给队列2绑定 routingKey 为 black 和 green
    @Bean("directBinding2")
    public Binding directBinding2(@Qualifier("directQueue2") Queue directQueue2, @Qualifier("directExchange") DirectExchange directExchange) {
        // 绑定队列和交换机
        return BindingBuilder.bind(directQueue2).to(directExchange).with("black");
    }

    @Bean("directBinding3")
    public Binding directBinding3(@Qualifier("directQueue2") Queue directQueue2, @Qualifier("directExchange") DirectExchange directExchange) {
        // 绑定队列和交换机
        return BindingBuilder.bind(directQueue2).to(directExchange).with("green");
    }
}
